/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.util.swing;

import java.util.List;

import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

/**
 * ListTableModel : The {@link IListModel}-based table model.
 * 
 * @author Romain DUBOIS
 * @param <R>
 *            The row objects type
 */
public class ListTableModel<R> extends DynamicTableModel<R> implements ListDataListener {

    /** Serial version */
    private static final long serialVersionUID = 1L;

    /** The inner list */
    private final IListModel<R> mData;

    /**
     * Constructor
     * 
     * @param pConverter
     *            The row converter
     */
    public ListTableModel(final IRowConverter<R> pConverter) {
        this(pConverter, new ArrayListModel<R>());
    }

    /**
     * Constructor
     * 
     * @param pConverter
     *            The row converter
     * @param pListModel
     *            The list model
     */
    public ListTableModel(final IRowConverter<R> pConverter, final IListModel<R> pListModel) {
        super(pConverter);

        mData = pListModel;
        mData.addListDataListener(this);
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.swing.table.TableModel#getRowCount()
     */
    @Override
    public int getRowCount() {
        return mData.size();
    }

    /**
     * Get the list data.
     * 
     * @return The list data
     */
    public List<R> getList() {
        return mData;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.swing.event.ListDataListener#intervalAdded(javax.swing.event.ListDataEvent)
     */
    @Override
    public void intervalAdded(final ListDataEvent pE) {
        fireTableRowsInserted(pE.getIndex0(), pE.getIndex1());
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.swing.event.ListDataListener#intervalRemoved(javax.swing.event.ListDataEvent)
     */
    @Override
    public void intervalRemoved(final ListDataEvent pE) {
        fireTableRowsDeleted(pE.getIndex0(), pE.getIndex1());
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.swing.event.ListDataListener#contentsChanged(javax.swing.event.ListDataEvent)
     */
    @Override
    public void contentsChanged(final ListDataEvent pE) {
        fireTableRowsUpdated(pE.getIndex0(), pE.getIndex1());
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.util.swing.DynamicTableModel#getRowValue(int)
     */
    @Override
    protected R getRowValue(final int pRowIndex) {
        return mData.get(pRowIndex);
    }
}
